package com.ebao.util.web;

import javax.servlet.http.HttpServletRequest;

/**
 * @author snow
 * @date 2012-5-24
 */
public class WebUtil {
	
	/**
	 * 获取登录用户
	 * @param request
	 * @return
	 */
//	public static User getLoginUser(HttpServletRequest request){
//		return (User)request.getSession().getAttribute("user");
//	}
	
	/**
	 * 获取登录Doctor
	 * @param request
	 * @return
	 */
//	public static Doctor getLoginDoctor(HttpServletRequest request){
//		return (Doctor)request.getSession().getAttribute("doctor");
//	}
	
	
	/***
     * 获取URI的路径,如路径为http://www.taobao.com/action/post.htm?method=add, 得到的值为"/action/post.htm"
     * @param request
     * @return
     */
    public String getRequestURI(HttpServletRequest request){     
        return request.getRequestURI();
    }
    
    /**
     * 获取完整请求路径(含内容路径及请求参数)
     * @param request
     * @return
     */
    public String getRequestURIWithParam(HttpServletRequest request){     
        return getRequestURI(request) + (request.getQueryString() == null ? "" : "?"+ request.getQueryString());
    }
    
    public String getAppURL(HttpServletRequest request) {
		StringBuffer url = new StringBuffer();
		int port = request.getServerPort();
		if (port < 0) {
			port = 80; // Work around java.net.URL bug
		}
		String scheme = request.getScheme();
		url.append(scheme);
		url.append("://");
		url.append(request.getServerName());
		if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) {
			url.append(':');
			url.append(port);
		}
		url.append(request.getContextPath());
		return url.toString();
	}

}
